<template>
  <div class="width">
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col
              :md="8"
              :sm="24"
              :key="index"
              v-for="(item, index) in searchForm.slice(0, advanced ? searchForm.length : 2)"
            >
              <a-form-item :label="item.label">
                <input-type
                  ref="inputType"
                  v-bind="item.props"
                  @change="(e) => (searchForm[index].value = e)"
                ></input-type>
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}">
                <a-button type="primary" @click="search">查询</a-button>
                <a-button style="margin-left: 8px" @click="empty">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px" v-if="searchForm.length > 2">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
  </div>
</template>

<script>
import InputType from '../InputType'
export default {
  components: {
    InputType
  },
  // eslint-disable-next-line vue/require-prop-types
  props: ['formData'],
  data () {
    return {
      advanced: false,
      searchForm: []
    }
  },
  created () {
    this.searchForm = this.formData
  },
  methods: {
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    search () {
      const { searchForm } = this
      const res = {}
      for (let i = 0; i < searchForm.length; i++) {
        res[searchForm[i].word] = searchForm[i].value === undefined ? '' : searchForm[i].value
      }
      this.$emit('search', res)
    },
    empty () {
      const { inputType } = this.$refs
      for (let i = 0; i < inputType.length; i++) {
        inputType[i].clear()
      }
      this.$emit('empty')
    }
  },
  watch: {
    formData () {
      this.searchForm = this.formData
    }
  }
}
</script>

<style lang="less" scoped>
.width {
  width: 100%;
}
</style>
